
function loadFriends() 
{	
	var params = {};
	params[opensocial.DataRequest.PeopleRequestFields.MAX] = 5000;	
	  var req = opensocial.newDataRequest();	
		req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS,params), 'viewer_friends');
	  req.send(onLoadFriends);
}

function onLoadFriends(data) {  
  var friends = new Array();  
  var viewerFriends = data.get('viewer_friends').getData();
  html = new Array();
  html.push('<select id="person">');
  viewerFriends.each(function(person) {
    html.push('<option value="' + person.getId() + '">' + person.getDisplayName() + "</option>");
    
    friends[person.getId()] = person.getDisplayName();
  });
  html.push('</select>');
  document.getElementById('friends').innerHTML = html.join('');  
}

function requestSendMessage(){
	
	var recipient = document.getElementById('person').value;
	 
	var params = {};
	params[opensocial.Message.Field.TITLE]="Hello";
	var body="Please Check This Application.";   
	var message = opensocial.newMessage(body, params);
	
	opensocial.requestSendMessage(recipient, message);
}

 function create_unique_random_array(num_elements,min,max) 
 {
		var temp, nums = new Array;
		for (var element=0; element < num_elements; element++) 
		{
			while ((temp = number_found ( random_number ( min, max ), nums )) == -1 )
			{
			}
			nums[element] = temp;
		}
		return (nums);
}
function number_found (random_number,number_array) 
{
		for (var element=0; element < number_array.length; element++) 
		{
			if (random_number == number_array[element]) 
			{
				return ( -1 );
			}
		}
		return ( random_number );
}
function random_number(min,max) 
{
    return (Math.round((max-min) * Math.random() + min));
}

function makeDOMRequest() 
{    
      var params = {};  
      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;  
      var url = "http://myapplication.googlecode.com/svn/trunk/questionsxml19.xml";  
      gadgets.io.makeRequest(url, response, params);
}

 function postActivity(text) {  
    var params = {};  
    params[opensocial.Activity.Field.TITLE] = text;
    var activity = opensocial.newActivity(params); 
    opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH,callback);
  };   
  
  function callback(status) {
    if (status.hadError())
    {
     // alert("Error creating activity.");
    }
    else 
    {
     // alert("Activity successfully created.");
    }
  };

function response(obj) 
{ 
      // obj.data contains a Document DOM element corresponding to the
      // page that was requested
      var domdata = obj.data;
      var title = domdata.getElementsByTagName("menu").item(0).getAttribute("title");
      var html = "<table style='background-color:#FFF;margin-left:25px;font-family:Verdana, Arial, Helvetica, sans-serif;' cellpadding='0' cellspacing='0' width='650px'>";
      html +="<tr><td colspan='2'><b>"+title+"</b>&nbsp;&nbsp;<input type='button' class='buttonAdd' name='btnReload' id='btnReload' value='Reload Quiz' onclick='init()' /></td></tr>";
	  html += "<tr><td style='height:10px;' colspan='2'></td></tr>";
      // Get a list of the <Question> element nodes in the file
      var itemList = domdata.getElementsByTagName("Question");
 
 	 var table1html = "<table style='background-color:#FFF;font-family:Verdana, Arial, Helvetica, sans-serif;' cellpadding='0' cellspacing='0' width='100%'>"
	 var table2html = "<table style='background-color:#FFF;font-family:Verdana, Arial, Helvetica, sans-serif;' cellpadding='0' cellspacing='0' width='100%'>"
 	var myNumArray = create_unique_random_array( 10, 1, 20 );
      // Loop through all <Question> nodes
      for (var i = 0; i < 10 ; i++) 
	  { 
        // For each <Question> node, get child nodes.
        var nodeList = itemList.item(myNumArray[i]).childNodes;

        // Loop through child nodes. Extract data from the text nodes that are
        // the children of the associated name, price, and calories element nodes.
        for (var j = 0; j < nodeList.length ; j++) 
		{		
          var node = nodeList.item(j);
          if (node.nodeName == "name") {
            var name = node.firstChild.nodeValue;			
			if(i > 4)
			{
				table2html += "<tr><td style='width:12px;'></td>";
				table2html += "<td style='width:300px;background-color:#59a9c3;color:#FFFFFF;padding:5px;padding-left:10px;font-size:11px;padding-top:2px;padding-bottom:2px;'><b>"+name+"</b></td><td style='width:25px;'></td><td style='width:12px;'></td></tr>";
			}
			else
			{
				table1html += "<tr><td style='width:12px;'></td>";
				table1html += "<td style='width:300px;background-color:#59a9c3;color:#FFFFFF;padding:5px;padding-left:10px;font-size:11px;padding-top:2px;padding-bottom:2px;'><b>"+name+"</b></td><td style='width:25px;'></td><td style='width:12px;'></td></tr>";
			}
			
			
          }
		  if (node.nodeName == "group") 
		  {
		  	 var groupnum = i + 1;
             var group = "group"+groupnum;
          }
		  
          if (node.nodeName == "option1") {
		  	var optval = node.firstChild.nodeValue;
			var pos = optval.indexOf("-");
			var val1 = optval.substring(pos+1);
			var option = optval.substring(0,pos);
			if(i > 4)
			{
				table2html += "<tr><td style='width:12px;'></td>";
				table2html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table2html += "<td style='width:25px;'></td>"; 
			}
			else
			{
				table1html += "<tr><td style='width:12px;'></td>";
				table1html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table1html += "<td style='width:25px;'></td>";         
			}
          }
		  
		   if (node.nodeName == "option2") 
		   {
		   var optval = node.firstChild.nodeValue;
			var pos = optval.indexOf("-");
			var val1 = optval.substring(pos+1);
			var option = optval.substring(0,pos);
			
			if(i > 4)
			{
				table2html += "<tr><td style='width:12px;'></td>";
				table2html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table2html += "<td style='width:25px;'></td>"; 
			}
			else
			{
				table1html += "<tr><td style='width:12px;'></td>";
				table1html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table1html += "<td style='width:25px;'></td>";         
			}
			
          }
		  if (node.nodeName == "option3") {
		  	var optval = node.firstChild.nodeValue;
			var pos = optval.indexOf("-");
			var val1 = optval.substring(pos+1);
			var option = optval.substring(0,pos);
			
			if(i > 4)
			{
				table2html += "<tr><td style='width:12px;'></td>";
				table2html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table2html += "<td style='width:25px;'></td>"; 
			}
			else
			{
				table1html += "<tr><td style='width:12px;'></td>";
				table1html += "<td style='width:300px;font-size:11px;'><input type='radio' name='"+group+"' value='"+val1+"' />"+option+"</td>";
				table1html += "<td style='width:25px;'></td>";         
			}			
          }
		 
        }
				
      }
	  table1html += "</table>";
	  table2html += "</table>";
	  html += "<tr><td style='width:50%;' valign='top'>"+table1html+"</td><td style='width:50%;' valign='top'>"+table2html+"</td></tr>";
      html += "<tr><td style='width:650px;height:10px;' colspan='2'></td></tr></table>";
      document.getElementById('content_div').innerHTML = html;
    };

function init() 
{		
  loadFriends(); 
  makeDOMRequest();
}
